package com.wgh.product.converter;

import com.wgh.product.dto.ProductRelationDTO;
import com.wgh.product.entity.shop.ProductRelation;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品关联转换器
 * 用于ProductRelation实体和ProductRelationDTO之间的转换
 */
@Component
public class ProductRelationConverter {

    /**
     * 将ProductRelation实体转换为ProductRelationDTO
     * @param productRelation ProductRelation实体
     * @return ProductRelationDTO
     */
    public ProductRelationDTO toDTO(ProductRelation productRelation) {
        if (productRelation == null) {
            return null;
        }
        
        ProductRelationDTO productRelationDTO = new ProductRelationDTO();
        productRelationDTO.setId(productRelation.getId());
        productRelationDTO.setMainProductId(productRelation.getMainProductId());
        productRelationDTO.setRelatedProductId(productRelation.getRelatedProductId());
        productRelationDTO.setRelationType(productRelation.getRelationType());
        productRelationDTO.setSort(productRelation.getSort());
        productRelationDTO.setCreateTime(productRelation.getCreateTime());
        productRelationDTO.setUpdateTime(productRelation.getUpdateTime());
        
        return productRelationDTO;
    }
    
    /**
     * 将ProductRelationDTO转换为ProductRelation实体
     * @param productRelationDTO ProductRelationDTO
     * @return ProductRelation实体
     */
    public ProductRelation toEntity(ProductRelationDTO productRelationDTO) {
        if (productRelationDTO == null) {
            return null;
        }
        
        ProductRelation productRelation = new ProductRelation();
        productRelation.setId(productRelationDTO.getId());
        productRelation.setMainProductId(productRelationDTO.getMainProductId());
        productRelation.setRelatedProductId(productRelationDTO.getRelatedProductId());
        productRelation.setRelationType(productRelationDTO.getRelationType());
        productRelation.setSort(productRelationDTO.getSort());
        
        return productRelation;
    }
    
    /**
     * 将ProductRelation实体列表转换为ProductRelationDTO列表
     * @param productRelations ProductRelation实体列表
     * @return ProductRelationDTO列表
     */
    public List<ProductRelationDTO> toDTOList(List<ProductRelation> productRelations) {
        if (productRelations == null || productRelations.isEmpty()) {
            return new ArrayList<>();
        }
        
        List<ProductRelationDTO> productRelationDTOs = new ArrayList<>();
        for (ProductRelation productRelation : productRelations) {
            productRelationDTOs.add(toDTO(productRelation));
        }
        
        return productRelationDTOs;
    }
    
    /**
     * 将ProductRelationDTO列表转换为ProductRelation实体列表
     * @param productRelationDTOs ProductRelationDTO列表
     * @return ProductRelation实体列表
     */
    public List<ProductRelation> toEntityList(List<ProductRelationDTO> productRelationDTOs) {
        if (productRelationDTOs == null || productRelationDTOs.isEmpty()) {
            return new ArrayList<>();
        }
        
        List<ProductRelation> productRelations = new ArrayList<>();
        for (ProductRelationDTO productRelationDTO : productRelationDTOs) {
            productRelations.add(toEntity(productRelationDTO));
        }
        
        return productRelations;
    }
}